package com.sdruixinggroup.mondayb2b.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.widget.Toast;

import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.widget.CPProgressDialog;


/**
 * 
 * 基础activity
 */
public class BaseActivity extends FragmentActivity {

	/**
	 * toast提示
	 */
	private Toast mToast;

	/**
	 * 通用弹框
	 */
	private CPProgressDialog mDialog;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	/**
	 * toast提示
	 *
	 * @param msg
	 *            待提示内容
	 */
	public void showToast(String msg) {
		if (TextUtils.isEmpty(msg)) {
			return;
		}
		if (mToast == null) {
			mToast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
		} else {
			mToast.setText(msg);
		}
		mToast.show();
	}

	/**
	 * toast提示
	 */
	public void showResultErrorToast() {
		showToast(getResources().getString(R.string.network_error));
	}

	/**
	 * 弹出对话框
	 *
	 * @param msg
	 *            待展示message
	 * @param isCancelable
	 *            是否可取消
	 * @param cancelListener
	 *            取消监听器
	 */
	public void showDialog(String msg, boolean isCancelable,
			DialogInterface.OnCancelListener cancelListener) {
		dismissDialog();
		if (mDialog == null) {
			mDialog = new CPProgressDialog(this);
		}
		if (!TextUtils.isEmpty(msg)) {
			mDialog.setMessage(msg);
		} else {
			mDialog.setMessage("");
		}
		mDialog.setCancelable(isCancelable);
		mDialog.setOnCancelListener(cancelListener);
		mDialog.show();
	}

	/**
	 * 弹出对话框
	 *
	 * @param msg
	 *            待展示message
	 */
	public void showDialog(String msg) {
		if (TextUtils.isEmpty(msg)) {
			showDialog("正在加载中...", false, null);
			return;
		}
		showDialog(msg, false, null);
	}

	/**
	 * dismiss对话框
	 */
	public void dismissDialog() {
		if (mDialog != null && mDialog.isShowing()) {
			mDialog.dismiss();
		}
		mDialog = null;
	}

	@Override
	protected void onDestroy() {
		dismissDialog();
		super.onDestroy();
	}

}
